//
//  ActionDetailsViewController.m
//  smarthings
//
//  Created by Stefan Tsvyatkov on 10/26/14.
//  Copyright (c) 2014 Stefan Tsvyatkov. All rights reserved.
//

#import "ActionDetailsViewController.h"
#import "ListOptionsViewController.h"
#import <MBProgressHUD.h>

@interface ActionDetailsViewController () <ListOptionsDelegate,SmarthingsDelegate>
@property (weak, nonatomic) IBOutlet UITableViewCell *actionTypeCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *descriptionCell;
@property (weak, nonatomic) IBOutlet UIButton *executeButton;
@property (strong, nonatomic) Smarthings *smarthingsSDK;

@end

@implementation ActionDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.smarthingsSDK = [Smarthings new];
    self.smarthingsSDK.delegate = self;
    if (self.isNewThing) {
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonTapped)];
        self.navigationItem.leftBarButtonItem = cancelButton;
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped)];
        self.navigationItem.rightBarButtonItem = doneButton;
        self.actionTypeCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.actionTypeCell.userInteractionEnabled = YES;
        self.executeButton.hidden = YES;
    }
    else {
        self.actionTypeCell.accessoryType = UITableViewCellAccessoryNone;
        self.actionTypeCell.userInteractionEnabled = NO;
        self.executeButton.hidden = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.actionTypeCell.detailTextLabel.text = self.action.actionType.uuid;
    self.descriptionCell.detailTextLabel.text = self.action.actionType.name;
}

- (void)cancelButtonTapped {
    [self.actionsArray removeObject:self.action];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)doneButtonTapped {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)executeButtonTapped:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.tabBarController.view animated:YES];
    [self.smarthingsSDK executeAction:self.action];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"SelectActionType"]) {
        [(ListOptionsViewController *)[segue destinationViewController] setDelegate:self];
        [(ListOptionsViewController *)[segue destinationViewController] setList:[Smarthings thingActionTypes]];
        [(ListOptionsViewController *)[segue destinationViewController] setSelectedIndex:[[Smarthings thingActionTypes] indexOfObject:self.action.actionType]];
        [[(ListOptionsViewController *)[segue destinationViewController] navigationItem] setTitle:@"Select Action Type"];
    }
}

- (void)indexHasBeenChosen:(NSInteger)index {
    self.action.actionType = [Smarthings thingActionTypes][index];
    self.action.name = self.action.actionType.uuid;
}

#pragma mark - Smarthings Delegate

- (void)executeActionFinishedWithFailure:(NSError *)error {
    [MBProgressHUD hideHUDForView:self.tabBarController.view animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Executing Action"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}

- (void)executeActionFinishedWithSuccess:(SMTThingAction *)action {
    [MBProgressHUD hideHUDForView:self.tabBarController.view animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success"
                                                        message:@"The action has been successfully executed."
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];
}

@end
